package com.qriver.cloud.consumer;

import com.sun.istack.internal.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 测试接口
 */
@RestController
public class ConsumerController {

    Logger logger = Logger.getLogger(ConsumerController.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/consumer")
    public String consumer() {
        logger.info("执行消费者ConsumerController的consumer()方法！");
        List<ServiceInstance> instances = discoveryClient.getInstances("eureka-provider");
        if (instances != null && instances.size() > 0) {
            ServiceInstance instance = instances.get(0);
            String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/provider";
            String re = restTemplate.getForObject(url, String.class);
            return re;
        }
        return "没有发现eureka-provider服务提供者！";
    }
}
